import Vue from 'vue'
import Vuex from 'vuex'
// import { assign } from 'core-js/fn/object'

Vue.use(Vuex)

const store =  new Vuex.Store({
  state: {
   addList:[],
   cart:[
           {
            id:123,
            price:200,
            imgurl:'https://bfs.biyao.com/group1/M00/6B/3A/rBACVF5WfbWAH-TXAALYhJO9erw125_800x800.jpg',
            // ml:'x',
            checked:false,
            name:'only小白瓶',
            shopname:'only旗舰店',
            digit:2,
          
            
          },
           {
            id:1234,
            price:200,
            imgurl:'https://bfs.biyao.com/group1/M00/6B/3A/rBACVF5WfbWAH-TXAALYhJO9erw125_800x800.jpg',
            // ml:'x',
            checked:false,
            name:'only小白瓶',
            shopname:'only旗舰店',
            digit:2,
           
          }   
   ],
   paygoods:[],
  },


  mutations: {
    addCart(state , cartList){
      let shop = ''
      state.cart.forEach((element , index) => {
        for( let key in element ){
          if( key === cartList.shopname ){
            state[index] = {
              ...element,
              cartList
            }
          }
          
          
        }
        
      });

      // if( cartList.shopname ===  )
      // for( let key in state.cart ){
      //   if( key === cartList.shopid ){
      //     // if(cartList.id)
      //     state.cart[shopid].push(cartList)
      //   }else{
      //     let setList = new Set({...cartList , num:1});
      //     state.cart[shopid].push(setList)
      //   }
      // }
      
    },
    addpayGoods(state , payload){
          state.paygoods.forEach( item => {
            if( item.id === payload.commodity.id ){
              return false
            }else{
              state.paygoods.push(payload.commodity)
            }
          } )
          
    }
  },
  actions: {
    addCart(context , cartList){
      context.commit('addCart' , cartList )
    },
    addpayGoods(context , payload){
      context.commit('addpayGoods' , payload)
    }
  },
  modules: {
  }
})
export default store;
